#include "AnimationData.h"

namespace binge
{

AnimationData::AnimationData(void)
{

}

AnimationData::~AnimationData(void)
{
    objectsName.clear();
    objectsFrameStart.clear();
    objectsAnimSetList.clear();
}

void
AnimationData::AddObject(int id)
{
    objectsId.push_back(id);
    objectsName.push_back("NotSetYet");
    objectsFrameStart.push_back(0);
    objectsAnimSetIndex.push_back(0);
    objectsSheet.push_back(SpriteSheet());
    objectsAnimSetList.push_back(std::vector<AnimationSet>());
}

void
AnimationData::RemoveObject(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsId.erase(objectsId.begin()+index);
        objectsName.erase(objectsName.begin()+index);
        objectsFrameStart.erase(objectsFrameStart.begin()+index);
        objectsAnimSetIndex.erase(objectsAnimSetIndex.begin()+index);
        objectsSheet.erase(objectsSheet.begin()+index);
        objectsAnimSetList.erase(objectsAnimSetList.begin()+index);
    }
}



void
AnimationData::SetName(int id, std::string name)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsName[index] = name;
        for (unsigned int x = 0; x < objectsAnimSetList[index].size(); x++)
        {
            if (objectsAnimSetList[index][x].GetName() == name)
            {
                objectsAnimSetIndex[index] = x;
                x = objectsAnimSetList[index].size();
            }
        }
    }
}

std::string
AnimationData::GetName(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsName[index];
    }
    return "";
}

void
AnimationData::SetCurrentFrame(int id, int num)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsAnimSetList[index][objectsAnimSetIndex[index]].SetCurrentFrame(num);
    }
}

int
AnimationData::GetCurrentFrame(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsAnimSetList[index][objectsAnimSetIndex[index]].GetCurrentFrame();
    }
    return 0;
}

void
AnimationData::AddAnimSet(int id, AnimationSet animSet)
{
    int index = GetIndex(id);
    if (index != -1)
    {

        objectsAnimSetList[index].push_back(animSet);
        objectsName[index] = objectsAnimSetList[index][objectsAnimSetList[index].size()-1].GetName();
    }
}

std::vector<AnimationSet>
AnimationData::GetAnimSetList(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsAnimSetList[index];
    }
    return std::vector<AnimationSet>(); // Default value.
}

bool
AnimationData::NextFrame(int id)
{
    bool hasRestarted = false;
    int index = GetIndex(id);
    if (index != -1)
    {
        hasRestarted = objectsAnimSetList[index][objectsAnimSetIndex[index]].NextFrame();
    }

    return hasRestarted;
}

void
AnimationData::SetSheet(int id, SpriteSheet sheet)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        objectsSheet[index] = sheet;
    }
}

SpriteSheet&
AnimationData::GetSheet(int id)
{
    int index = GetIndex(id);
    if (index != -1)
    {
        return objectsSheet[index];
    }
}

void
AnimationData::Animate(void)
{
    for (int j = 0; j < GetCount(); j++)
    {
        unsigned int frameTimer = 0;

        // update frame based on time
        frameTimer = objectsAnimSetList[j][objectsAnimSetIndex[j]].GetFrameTimer();
        if (frameTimer > 0)
        {
            if (timer.GetTimer() > objectsFrameStart[j] + frameTimer)
            {
                //reset animation timer
                objectsFrameStart[j] = timer.GetTimer();
                objectsAnimSetList[j][objectsAnimSetIndex[j]].NextFrame();
            }
        }
        else
        {
            //no animation timer--update at cpu clock speed
            objectsAnimSetList[j][objectsAnimSetIndex[j]].NextFrame();
        }
        objectsSheet[j].SetFrameIndex(objectsAnimSetList[j][objectsAnimSetIndex[j]].GetCurrentFrame());
    }
}

} // namespace
